<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>扫小票demo</title>
		<link href="css/mui.min.css" rel="stylesheet" />
		<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

		<style>
			ul {
				font-size: 14px;
				color: #8f8f94;
			}
			
			.mui-btn {
				padding: 10px;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav" style="width: 100%;">
			<a href="main.html" class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">拍照</h1>
		</header>
		<div class="mui-content">
			<div class="mui-content-padded">

				<img id="headimg" src='' height="150" width="150">
				<p>
					<a href="javascript:;" class="imageup">上传图片</a>
				</p>
				<p><button onclick="upload();" class='button'> 提交</button></p>

			</div>
		</div>
		<script src="js/mui.min.js"></script>
		<script src="js/app.js"></script>
		<script type="text/javascript" src="js/common.js"></script>
		<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
		<script src="https://cdn.bootcss.com/layer/3.0.1/layer.min.js"></script>

		<script>
			function plusReady() {
				// 弹出系统选择按钮框  
				mui("body").on("tap", ".imageup", function() {
					page.imgUp();
				})

			}
			var page = null;
			page = {
				imgUp: function() {
					var m = this;
					plus.nativeUI.actionSheet({
						cancel: "取消",
						buttons: [{
								title: "拍照"
							},
							{
								title: "从相册中选择"
							}
						]
					}, function(e) { //1 是拍照  2 从相册中选择  
						switch(e.index) {
							case 1:
								appendByCamera();
								break;
							case 2:
								appendByGallery();
								break;
						}
					});
				}
				//摄像头  
			}

			// 拍照添加文件
			function appendByCamera() {
				plus.camera.getCamera().captureImage(function(e) {
					console.log(e);
					plus.io.resolveLocalFileSystemURL(e, function(entry) {
						var path = entry.toLocalURL();
						document.getElementById("headimg").src = path;
						//就是这里www.bcty365.com 
					}, function(e) {
						mui.toast("读取拍照文件错误：" + e.message);
					});

				});
			}
			// 从相册添加文件
			function appendByGallery() {
				plus.gallery.pick(function(path) {
					document.getElementById("headimg").src = path;

				});
			}

			//服务端接口路径
			var server = "/fenful/api/v1/common/ocr";
			//获取图片元素
			var files = document.getElementById('headimg');
			// 上传文件
			function upload() {
				console.log(files.src);
				var wt = plus.nativeUI.showWaiting();
				var task = plus.uploader.createUpload(server, {
						method: "POST"
					},
					function(t, status) { //上传完成
						if(status == 200) {
							alert("上传成功：" + t.responseText);
							wt.close(); //关闭等待提示按钮
						} else {
							alert("上传失败：" + status);
							wt.close(); //关闭等待提示按钮
						}
					}
				);
				//添加其他参数
				task.addData("name", "test");
				task.addFile(files.src, {
					key: "dddd"
				});
				task.start();
			}
			if(window.plus) {
				plusReady();
			} else {
				document.addEventListener("plusready", plusReady, false);
			}
		</script>
	</body>

</html>